using System.Collections.Generic;
using System.Windows;
using System.Windows.Markup;
using Common;
using Infragistics.Win.UltraWinGrid;

namespace MainView.Windows
{
	public partial class CustomFilterWindow : Window, IComponentConnector
	{
		public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register("Caption", typeof(string), typeof(CustomFilterWindow), new UIPropertyMetadata(null));

		public static readonly DependencyProperty LogicOperatorProperty = DependencyProperty.Register("LogicOperator", typeof(FilterLogicalOperator), typeof(CustomFilterWindow), new UIPropertyMetadata(FilterLogicalOperator.And));

		public static readonly DependencyProperty FirstOperatorProperty = DependencyProperty.Register("FirstOperator", typeof(FilterComparisionOperator), typeof(CustomFilterWindow), new UIPropertyMetadata(null));

		public static readonly DependencyProperty FirstValueProperty = DependencyProperty.Register("FirstValue", typeof(object), typeof(CustomFilterWindow), new UIPropertyMetadata(null));

		public static readonly DependencyProperty SecondOperatorProperty = DependencyProperty.Register("SecondOperator", typeof(FilterComparisionOperator), typeof(CustomFilterWindow), new UIPropertyMetadata(null));

		public static readonly DependencyProperty SecondValueProperty = DependencyProperty.Register("SecondValue", typeof(object), typeof(CustomFilterWindow), new UIPropertyMetadata(null));

		public static readonly DependencyProperty ItemsSourctProperty = DependencyProperty.Register("ItemsSourct", typeof(List<KeyValuePair>), typeof(CustomFilterWindow), new UIPropertyMetadata(null));

		public static readonly DependencyProperty NumberFilterProperty = DependencyProperty.Register("NumberFilter", typeof(bool?), typeof(CustomFilterWindow), new UIPropertyMetadata(null, OnNumberFilterChanged));

		public string Caption
		{
			get
			{
				return (string)GetValue(CaptionProperty);
			}
			set
			{
				SetValue(CaptionProperty, value);
			}
		}

		public FilterLogicalOperator LogicOperator
		{
			get
			{
				return (FilterLogicalOperator)GetValue(LogicOperatorProperty);
			}
			set
			{
				SetValue(LogicOperatorProperty, value);
			}
		}

		public FilterComparisionOperator FirstOperator
		{
			get
			{
				return (FilterComparisionOperator)GetValue(FirstOperatorProperty);
			}
			set
			{
				SetValue(FirstOperatorProperty, value);
			}
		}

		public object FirstValue
		{
			get
			{
				return GetValue(FirstValueProperty);
			}
			set
			{
				SetValue(FirstValueProperty, value);
			}
		}

		public FilterComparisionOperator SecondOperator
		{
			get
			{
				return (FilterComparisionOperator)GetValue(SecondOperatorProperty);
			}
			set
			{
				SetValue(SecondOperatorProperty, value);
			}
		}

		public object SecondValue
		{
			get
			{
				return GetValue(SecondValueProperty);
			}
			set
			{
				SetValue(SecondValueProperty, value);
			}
		}

		public List<KeyValuePair> ItemsSourct
		{
			get
			{
				return (List<KeyValuePair>)GetValue(ItemsSourctProperty);
			}
			set
			{
				SetValue(ItemsSourctProperty, value);
			}
		}

		public bool? NumberFilter
		{
			get
			{
				return (bool)GetValue(NumberFilterProperty);
			}
			set
			{
				SetValue(NumberFilterProperty, value);
			}
		}

		private static void OnNumberFilterChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			CustomFilterWindow customFilterWindow = sender as CustomFilterWindow;
			if (customFilterWindow != null)
			{
				customFilterWindow.InitItemsSource();
			}
		}

		private void InitItemsSource()
		{
			List<KeyValuePair> list = new List<KeyValuePair>();
			if (NumberFilter == true)
			{
				list.Add(new KeyValuePair
				{
					Key = FilterComparisionOperator.Custom,
					Value = string.Empty
				});
				list.Add(new KeyValuePair
				{
					Key = FilterComparisionOperator.Equals,
					Value = Utility.GetMessage("operator_equal")
				});
				list.Add(new KeyValuePair
				{
					Key = FilterComparisionOperator.NotEquals,
					Value = Utility.GetMessage("operator_unequal")
				});
				list.Add(new KeyValuePair
				{
					Key = FilterComparisionOperator.LessThan,
					Value = Utility.GetMessage("operator_less")
				});
				list.Add(new KeyValuePair
				{
					Key = FilterComparisionOperator.LessThanOrEqualTo,
					Value = Utility.GetMessage("operator_less_equal")
				});
				list.Add(new KeyValuePair
				{
					Key = FilterComparisionOperator.GreaterThan,
					Value = Utility.GetMessage("operator_more")
				});
				list.Add(new KeyValuePair
				{
					Key = FilterComparisionOperator.GreaterThanOrEqualTo,
					Value = Utility.GetMessage("operator_more_equal")
				});
			}
			else if (NumberFilter == false)
			{
				list.Add(new KeyValuePair
				{
					Key = FilterComparisionOperator.Custom,
					Value = string.Empty
				});
				list.Add(new KeyValuePair
				{
					Key = FilterComparisionOperator.Equals,
					Value = Utility.GetMessage("operator_equal")
				});
				list.Add(new KeyValuePair
				{
					Key = FilterComparisionOperator.Contains,
					Value = Utility.GetMessage("operator_contains")
				});
				list.Add(new KeyValuePair
				{
					Key = FilterComparisionOperator.StartsWith,
					Value = Utility.GetMessage("operator_tartswith")
				});
				list.Add(new KeyValuePair
				{
					Key = FilterComparisionOperator.EndsWith,
					Value = Utility.GetMessage("operator_endswith")
				});
			}
			comboBox1.ItemsSource = list;
			comboBox2.ItemsSource = list;
		}

		public CustomFilterWindow()
		{
			InitializeComponent();
		}

		private void btnOk_Click(object sender, RoutedEventArgs e)
		{
			base.DialogResult = true;
			Close();
		}

		private void btnCancel_Click(object sender, RoutedEventArgs e)
		{
			Close();
		}

		private void btnClearFilter_Click(object sender, RoutedEventArgs e)
		{
			ClearFilter();
		}

		public void ClearFilter()
		{
			ClearFirstFilter();
			ClearSecondFilter();
		}

		public void ClearFirstFilter()
		{
			FirstOperator = FilterComparisionOperator.Custom;
			FirstValue = null;
		}

		public void ClearSecondFilter()
		{
			SecondOperator = FilterComparisionOperator.Custom;
			SecondValue = null;
		}

		public bool HasFirstFilter()
		{
			return FirstOperator != FilterComparisionOperator.Custom;
		}

		public bool HasSecondFilter()
		{
			return SecondOperator != FilterComparisionOperator.Custom;
		}
	}
}
